package com.dins.elashutina.reporter;


import com.dins.elashutina.parser.Node;
import com.dins.elashutina.parser.Tree;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: ekaterina.lashutina
 * Date: 04.09.13
 * Time: 17:07
 */
public class Reporter {

    public static void bullseyeReport(Tree tree) {
        InputStream inp;
        try {
            inp = new FileInputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\fulldata.xls");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            for (int index = sheet.getLastRowNum(); index >= sheet.getFirstRowNum(); index--) {
                if (sheet.getRow(index) == null) {
                    break;
                }
                sheet.removeRow(sheet.getRow(index));
            }
            wb.setSheetName(0, "Coverage bullseyeReport");

            Row row0 = sheet.createRow(0);
            row0.createCell(0).setCellValue("Component");
            row0.createCell(1).setCellValue("fn_cov");
            row0.createCell(2).setCellValue("fn_total");
            row0.createCell(3).setCellValue("cd_cov");
            row0.createCell(4).setCellValue("cd_total");
            row0.createCell(5).setCellValue("d_cov");
            row0.createCell(6).setCellValue("d_total");
            int i = 1;
            for (Node node : tree) {
                Row row = sheet.createRow(i);
                row.createCell(0).setCellValue(node.getName());
                row.createCell(1).setCellValue(node.getValue(0));
                row.createCell(2).setCellValue(node.getValue(1));
                row.createCell(3).setCellValue(node.getValue(2));
                row.createCell(4).setCellValue(node.getValue(3));
                row.createCell(5).setCellValue(node.getValue(4));
                row.createCell(6).setCellValue(node.getValue(5));
                row.createCell(7).setCellValue(node.getPath());
                i++;
            }
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\fulldata.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void coberturaReport(Tree tree) {
        InputStream inp;
        try {
            inp = new FileInputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\fulldata.xls");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            wb.setSheetName(0, "Coverage coberturaReport");
            for (int index = sheet.getLastRowNum(); index >= sheet.getFirstRowNum(); index--) {
                if (sheet.getRow(index) == null) {
                    continue;
                }
                sheet.removeRow(sheet.getRow(index));
            }
            Row row0 = sheet.createRow(0);
            row0.createCell(0).setCellValue("Component");
            row0.createCell(1).setCellValue("line-rate");
            row0.createCell(2).setCellValue("branch-rate");
            row0.createCell(3).setCellValue("path");
            int i = 1;
            for (Node node : tree) {
                Row row = sheet.createRow(i);
                row.createCell(0).setCellValue(node.getName());
                row.createCell(1).setCellValue(node.getValue(0));
                row.createCell(2).setCellValue(node.getValue(1));
                row.createCell(3).setCellValue(node.getPath());
                i++;
            }
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\fulldata.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void jaCoCoReport(Tree tree) {
        InputStream inp;
        try {
            inp = new FileInputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\fulldata.xls");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            wb.setSheetName(0, "Coverage jaCoCoReport");
            for (int index = sheet.getLastRowNum(); index >= sheet.getFirstRowNum(); index--) {
                if (sheet.getRow(index) == null) {
                    continue;
                }
                sheet.removeRow(sheet.getRow(index));
            }
            Row row0 = sheet.createRow(0);
            row0.createCell(0).setCellValue("Component");
            row0.createCell(1).setCellValue("INSTRUCTION");
            row0.createCell(2).setCellValue("BRANCH");
            row0.createCell(3).setCellValue("LINE");
            row0.createCell(4).setCellValue("COMPLEXITY");
            row0.createCell(5).setCellValue("METHOD");
            row0.createCell(6).setCellValue("CLASS");
            row0.createCell(7).setCellValue("Path");
            int i = 1;
            for (Node node : tree) {
                Row row = sheet.createRow(i);
                row.createCell(0).setCellValue(node.getName());
                row.createCell(1).setCellValue(node.getValue(0));
                row.createCell(2).setCellValue(node.getValue(1));
                row.createCell(3).setCellValue(node.getValue(2));
                row.createCell(4).setCellValue(node.getValue(3));
                row.createCell(5).setCellValue(node.getValue(4));
                row.createCell(6).setCellValue(node.getValue(5));
                row.createCell(7).setCellValue(node.getPath());
                i++;
            }
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\fulldata.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
